define(["jquery","bbcare"], function ($,$bb) {
    var URL = $bb.baseUrl("nutrition");
    var servers = {};

    /**
     * 请求营养方案列表
     */
    servers.ajaxSchemeList = function(callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/scheme/querylist",
            data: JSON.stringify({
                data: {}
            }),
            success: function (data) {
                if(data.success){
                    callback(data.rows);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "营养方案列表出错！");
                }
            }
        });
    };

    /**
     * 处理营养方案列表的数据
     */
    servers.handleSchemeList = function(list){
        for (var i = 0; i < list.length; i++) {
            if(!list[i].createName) list[i].createName = "平台方案";
            list[i].createTime = list[i].createTime.substr(0,10);
        }
        return {
            "list":list
        };
    }

    /**
     * 获取修改营养方案的原数据
     */
    servers.getSetSchemeOriginalData = function(data,schemeId){
        for (var i = 0; i < data.length; i++) {
            if( data[i].schemeId == schemeId ){
                return data[i];
            }
        }
    }

    /**
     * 获取新建/修改营养方案的参数
     */
    servers.getSchemeBasicModifyParams = function(dom){
        var schemeName = $("#schemeName").val();
        var usingTime = $("#usingTime").val();
        var usingTimeUnit = $("#usingTimeUnit").val();
        var adaptMan = $("#adaptMan").val();
        var schemeAim = $("#schemeAim").val();
        var schemeId = dom.attr("schemeId");
        var days;
        switch(usingTimeUnit){
            case "day":
                days = Number(usingTime);
                break;
            case "week":
                days = Number(usingTime)*7;
                break;
            case "month":
                days = Number(usingTime)*30;
                break;
            case "year":
                days = Number(usingTime)*365;
                break;
        }
        return {
            "schemeName":schemeName,
            "days":days.toString(),
            "schemeApply":adaptMan,
            "schemeDesc":schemeAim,
            "schemeId":schemeId  //传入则代表修改
        };
    }

    /**
     * 请求新建/修改营养方案
     */
    servers.ajaxSchemeBasicModify = function(params,callback,className){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/schemebasic/modify",
            data: JSON.stringify({
                data: params
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data,params.schemeId,className);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "新建/修改营养方案出错！");
                }
            }
        });
    };

    /**
     * 方案复制
     */
    servers.schemeClone = function(schemeId,callback){
        $bb.ajax({
            url: URL + "nutritiouScheme/service/nutrition/scheme/clone",
            data: JSON.stringify({
                data: {
                    "schemeId":schemeId
                }
            }),
            success: function (data) {
                if(data.success){
                    callback(data.data);
                }else{
                    $bb.toastShow(data.msg ? data.msg : "方案复制出错！");
                }
            }
        });
    }

    return servers;
});
